package de.fmannan.addbook.views;


import org.apache.log4j.Logger;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.DrillDownAdapter;
import org.eclipse.ui.part.ViewPart;

import de.fmannan.addbook.MyImageRegistry;
import de.fmannan.addbook.MyMessageRegistry;
import de.fmannan.addbook.domainmodel.Group;
import de.fmannan.addbook.editor.OpenEditorActionDelegate;


/**
 * @author fmannan
 * 
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class ContactTreeView extends ViewPart {
	
	public static final String ID = "de.fmannan.addbook.views.ContactTreeView";
	private static Logger log = Logger.getLogger(ContactTreeView.class);
	private static ContactTreeView self = null;
	private TreeViewer viewer;
	private DrillDownAdapter drillDownAdapter;
//	private Action addContact;
	private Action addGroup;
	private Action doubleClickAction;
	 
	/**
	 * The constructor.
	 */
	public ContactTreeView() {
		self = this;
	}
	
	public static ContactTreeView getInstance() {
		return self;
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);
		drillDownAdapter = new DrillDownAdapter(viewer);
		viewer.setContentProvider(new TreeViewContentProvider(this));
		viewer.setLabelProvider(new TreeViewLabelProvider(this));
		viewer.setSorter(new ContactSorter());
		viewer.setInput(getViewSite());
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				ContactTreeView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
//		manager.add(addContact);
//		manager.add(new Separator());
		manager.add(addGroup);
	}

	private void fillContextMenu(IMenuManager manager) {
//		manager.add(addContact);
		manager.add(addGroup);
//		manager.add(new Separator());
//		drillDownAdapter.addNavigationActions(manager);
		// Other plug-ins can contribute their actions here
//		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}
	
	private void fillLocalToolBar(IToolBarManager manager) {
//		manager.add(addContact);
		manager.add(addGroup);
//		manager.add(new Separator());
//		drillDownAdapter.addNavigationActions(manager);
	}

	/**
	 * Actions Items placed within a Popup menu integrated into the tree view.
	 */
	private void makeActions() {
		//Context menu for adding new Groups
		addGroup = new Action() {
			public void run() {
				// A new editor windows should be opened in order to create a
				// new Group
				OpenEditorActionDelegate.openEditor(PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getActivePage(), new Group(""));	
			}
		};
		
		addGroup.setText(MyMessageRegistry.NEW_GROUP_TITLE);
		addGroup.setToolTipText("Allows you to create a new Group");
		// getImageDescriptor allows you to set an image for the action button
		addGroup.setImageDescriptor(MyImageRegistry.getImageRegistry()
				.getDescriptor(MyImageRegistry.GROUP_ICON));
		
		
		doubleClickAction = new Action() {
			public void run() {
				
				log.debug("Double clicked on a Group - opening editor ...");
				ISelection selection = viewer.getSelection();
				// A new editor windows should be opened in order to edit an
				// existing group
				OpenEditorActionDelegate.openEditor(PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getActivePage(), selection);
			}
		};
	}

	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				doubleClickAction.run();
			}
		});
	}
	
	private void showMessage(String message) {
		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"Contact View",
			message);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
	
	public void refresh() {
		viewer.refresh();
	}
}